import CONSTANT from '../../constant';

export default function(poster) {
    
    window.addEventListener('load', () => {
        // 记录页面加载完成时间
        poster.sendDataByImg({uuid:CONSTANT.uuid, type:'page', subType:'onload', stime: Date.now()});
    }, true);

    window.addEventListener('beforeunload', () => {
        // 记录页面加载完成时间
        poster.sendDataByImg({uuid:CONSTANT.uuid, type:'page', subType:'beforeunload', stime: Date.now()});
    }, true);

    window.addEventListener('hashchange',() => {
        const hash = location.hash.substring(1);
        poster.sendDataByImg({uuid:CONSTANT.uuid, type:'page', subType:'hashchange', stime: Date.now(), hash: hash});
    });

    document.addEventListener("click", (e) => {
        try {
            // 如果目标节点存在事件前缀 则继续
            if (e.target.id && e.target.id.startsWith(CONSTANT.EVENT_PREFIX)) {
                // 上报数据
                const eventName = e.target.id.split(CONSTANT.EVENT_PREFIX)[1];
                let eventContent = e.target.innerHTML || "";
                eventContent = eventContent.length > 50? eventContent.substring(0,50):eventContent;
                //上传埋点信息fn
                poster.sendDataByImg({uuid:CONSTANT.uuid, type:'page', subType:'click', stime: Date.now(), name: eventName, content: encodeURIComponent(encodeURIComponent(eventContent))});
            }
        } catch (e) {

        }
    });

}